package com.vcredit.creditcat.web;

import com.vcredit.creditcat.framework.pagination.DefaultCriteria;
import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.model.ChannelStatisticsOwner;
import com.vcredit.creditcat.model.Resource;
import com.vcredit.creditcat.security.SecurityHelper;
import com.vcredit.creditcat.service.ChannelStatisticsOwnerService;
import com.vcredit.creditcat.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/channelCount")
public class ChannelCountController extends BaseController {
	@Autowired
	private ChannelStatisticsOwnerService channelStatisticsOwnerService;
	@Autowired
	private ResourceService resourceService;

	@RequestMapping("/channelList")
	public ModelAndView showPageFeedback(HttpServletRequest request) {
		return new ModelAndView("/channelcount/channelCountList");
	}
	/**
	 * 根据当前权限查询 当前的统计
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/listChannelCount")
	public Page<ChannelStatisticsOwner> listChannelData(HttpServletRequest request) {
		Page<ChannelStatisticsOwner> channelStatisticsOwnerPage = null;
		try {
			//渠道数据查询权限
			List<Resource> channelData=resourceService.findByUserId(SecurityHelper.getUserId(),2);
			if(channelData==null || channelData.size()<1){
				return null;
			}
			String[] channelArray=new String[channelData.size()];
			for(int i=0;i<channelData.size();i++){
				channelArray[i]=channelData.get(i).getValue();
			}
			DefaultCriteria defaultCriteria=makeDefaultCriteria(request);
			Map<String,Object> params=defaultCriteria.getParams();
			params.put("channelArray",channelArray);
			channelStatisticsOwnerPage = channelStatisticsOwnerService.findByPage(defaultCriteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return channelStatisticsOwnerPage;
	}
}
